//myApp.init();

function ajaxfail(msg){
    if(msg){
        myApp.alert(msg);
    }else{
        myApp.alert("网络传输有问题，请重试或检查网络");
    }    
}

function checkuseragent(){
	var u = navigator.userAgent;
	//android终端或uc浏览器
	var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; 
	//ios终端 
	var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); 
	if(isiOS){
		return 'IOS';
	}else if(isAndroid){
		return 'Android';
	}else{
		return '';
	}
}

function checklogin(){
    $$.ajax({
        url:url_get_checklogin,
        method:"GET",
        data: {},
        dataType:'json',
        async:false,
        xhrFields:{withCredentials:true},
        // crossDomain:true,
        success: function (ajaxresult) {
        //获取数据
        	console.log(ajaxresult);
            if(ajaxresult.code==1){
                if(ajaxresult.data.is_login==1){
                    global_login_state = true;
                    global_login_role = ajaxresult.data.user_type;
                }else{
                    global_login_state = false;
                }                
            }else{
                global_login_state = false;
            }
        },
    });
}

function isscenicremarked(scenic_id){
	var res=false;
	$$.ajax({
        url:url_post_isscenicremarked,
        method:"post",
        data: {"scenic_id":scenic_id},
        dataType:'json',
        async:false,
        xhrFields:{withCredentials:true},
        success: function (ajaxresult) {
            if(ajaxresult.code==1){
                res=true;               
            }
        },
    });
	return res;
}

function resideselect(resideTime){
    switch(parseInt(resideTime))
    {
	    case 1:
	        return '<select name="resideTime"><option selected value="1">1年</option><option value="2">2年</option><option value="3">3年</option><option value="4">4年</option><option value="5">5年以上</option></select>';
	        break;
	    case 2:
	        return '<select name="resideTime"><option value="1">1年</option><option selected value="2">2年</option><option value="3">3年</option><option value="4">4年</option><option value="5">5年以上</option></select>';
	        break;
	    case 3:
	        return '<select name="resideTime"><option value="1">1年</option><option value="2">2年</option><option selected value="3">3年</option><option value="4">4年</option><option value="5">5年以上</option></select>';
	        break;
	    case 4:
	        return '<select name="resideTime"><option value="1">1年</option><option value="2">2年</option><option value="3">3年</option><option selected value="4">4年</option><option value="5">5年以上</option></select>';
	        break;
	    case 5:
	        return '<select name="resideTime"><option value="1">1年</option><option value="2">2年</option><option value="3">3年</option><option value="4">4年</option><option selected value="5">5年以上</option></select>';
	        break;
	    default:
	        return '<select name="resideTime"><option selected value="1">1年</option><option value="2">2年</option><option value="3">3年</option><option value="4">4年</option><option value="5">5年以上</option></select>';
	        break;
    }
}

function userageselect(userage){
	switch(userage)
    {
	    case '90后':
	        return '<select name="userAge"><option selected value="90后">90后</option><option value="80后">80后</option><option value="70后">70后</option><option value="00后">00后</option></select>';
	        break;
	    case '80后':
	        return '<select name="userAge"><option value="90后">90后</option><option selected value="80后">80后</option><option value="70后">70后</option><option value="00后">00后</option></select>';
	        break;
	    case '70后':
	    	return '<select name="userAge"><option value="90后">90后</option><option value="80后">80后</option><option selected value="70后">70后</option><option value="00后">00后</option></select>';
	        break;
	    case '00后':
            return '<select name="userAge"><option value="90后">90后</option><option value="80后">80后</option><option value="70后">70后</option><option selected value="00后">00后</option></select>';
	    	break;
	    default:
            return '<select name="userAge"><option value="90后">90后</option><option value="80后">80后</option><option value="70后">70后</option><option value="00后">00后</option></select>';
	    	break;
    }
}

function googlemap(address,myLatlng,infowidowcontent,mapcontainer,mapOptions){
        console.log('address:'+address+';'
        		   +'myLatlng:'+myLatlng+';'
        		   +'infowidowcontent:'+infowidowcontent+';'
        		   +'mapOptions:'+mapOptions+';');
        var infowindow,marker;
        //初始化地图
        var map = new google.maps.Map(mapcontainer,mapOptions);
        
        //ajax查询地址
        if(address){
        	var geocoder = new google.maps.Geocoder();
            geocoder.geocode( { 'address': address}, function(results, status) {
                console.log(results,status);
                if (status == google.maps.GeocoderStatus.OK) {
                	//将地图中心设为目标
                    map.setCenter(results[0].geometry.location);
                    //显示目标marker
                    marker = new google.maps.Marker({
                        map: map,
                        position: results[0].geometry.location
                    });
                    //设置信息窗
                    if(infowidowcontent){
                    	 infowindow = new google.maps.InfoWindow({
                             content:infowidowcontent,
                             maxWidth: 300
                          });
                          google.maps.event.addListener(marker, 'click', function() {
                              infowindow.open(map,marker);
                          });
                    }              
                } else {
                    ajaxfail('Googlemap was not successful for the following reason: ' + status);
                }
            });
        }
        //根据latlng确定位置
        else{
        	//显示目标marker
            marker = new google.maps.Marker({
                map: map,
                position: myLatlng
            });
            //设置信息窗
            if(infowidowcontent){
            	 infowindow = new google.maps.InfoWindow({
                     content:infowidowcontent,
                     maxWidth: 300
                  });
                  google.maps.event.addListener(marker, 'click', function() {
                      infowindow.open(map,marker);
                  });
            }
        }       
}
/*---生成行程线路---*/
function clearMarkers() {
	  for (var i = 0; i < markers.length; i++) {
	    markers[i].setMap(null);
	  }
	  markers = [];
}
function addMarkerWithTimeout(map, position, info, label, timeout) {
	  setTimeout(function() {
		//生成标记
		var marker=new google.maps.Marker({
		      position: position,
		      map: map,
		      label:label.toString(),
		      animation: google.maps.Animation.DROP
		});
	    //信息窗口
	    var infowindow=new google.maps.InfoWindow({
	        content:info,
	        maxWidth: 100
	     });
		marker.addListener('click', function() {
	    	infowindow.open(map,marker);
	     });
	  }, timeout);
}
function drop(map,linepath,infos) {
	  for (var i = 0; i < linepath.length; i++) {
	    addMarkerWithTimeout(map, linepath[i], infos[i], i+1, i * 500);
	  }
}
function googlemapline(linepath,infos,mapcontainer,mapOptions){
	   //初始化地图
	   var map = new google.maps.Map(mapcontainer,mapOptions);
	   //初始化线段
	   var line = new google.maps.Polyline({
		    path: linepath,
		    geodesic: true,
		    strokeColor: '#08d1a1',
		    strokeOpacity: 1.0,
		    strokeWeight: 2
		  });
	   //绘制线段
	   line.setMap(map);
	   
	   //标记动画
	   drop(map,linepath,infos);
	   
}
//时间格式由2016-09-05转换为2016-9-5
function getformatday(d){
    var t=new Date(d);
    var tm=new Date(t.getFullYear(),t.getMonth(),t.getDate());
    return tm.getFullYear() + '-' +(tm.getMonth()+1).toString() + '-' + tm.getDate();
}
//获取加上dadd天后的日期
function getthedate(dd,dadd){
    //可以加上错误处理
    var a = new Date(dd);
    a = a.valueOf();
    a = a + dadd * 24 * 60 * 60 * 1000;
    a = new Date(a);
    var m = a.getMonth() + 1;
    if(m.toString().length == 1){
        m='0'+m;
    }
    var d = a.getDate();
    if(d.toString().length == 1){
        d='0'+d;
    }
    return a.getFullYear() + "-" + m + "-" + d;
}

function getuserinfo(){
	var userinfo=false;
    $$.ajax({
        url:url_get_selfbaseinfo,
        method:"GET",
        async:false,
        dataType:'json',
        xhrFields:{withCredentials:true},
        success: function (ajaxresult) {
            if(ajaxresult.code==1){
                var user = ajaxresult.data;
                userinfo = user;
            }else{
            	ajaxfail(ajaxresult.msg);
            }
        }
    });
    return userinfo;
}

function getplaymethodlist(offset,limit,type){
	//搜索条件
	var playingmethod_city = $$('.playingmethodcity a.active').data('id');
	var playingmethod_type = $$('.playingmethodtype a.active').data('id');
	var playingmethod_sort = $$('.playingmethodsort a.active').data('id');
	var playingmethod_gender = $$('.playingmethodfilter .gender a.active').data('id');
	var playingmethod_age = $$('.playingmethodfilter .age a.active').data('id');
	var playingmethod_traffic = $$('.playingmethodfilter .traffic .active').data('id');
	var playingmethod_role = $$('.playingmethodfilter .role a.active').data('id');
	console.log(playingmethod_city,playingmethod_type,playingmethod_sort,playingmethod_gender,playingmethod_age,playingmethod_traffic,playingmethod_role);
	//收起subnav
	$$("#playingmethodlist").removeClass('with-shadow');
	if ($$('.playingmethodtype').hasClass('grade-w-roll')){
        $$('.playingmethodtype').removeClass('grade-w-roll');
    };
    if ($$('.playingmethodsort').hasClass('grade-w-roll')){
        $$('.playingmethodsort').removeClass('grade-w-roll');
    };
    if ($$('.playingmethodfilter').hasClass('grade-w-roll')){
        $$('.playingmethodfilter').removeClass('grade-w-roll');
    };
    if ($$('.playingmethodcity').hasClass('grade-w-roll')){
        $$('.playingmethodcity').removeClass('grade-w-roll');
    };
    
    //刷新列表
	$$.getJSON(url_get_playingmethodlist, {
        city:playingmethod_city,
        type:playingmethod_type,
        offset:offset,
        limit:limit,
        gender:playingmethod_gender,
        chuxing:playingmethod_traffic,
        age:playingmethod_age,
        identity:playingmethod_role,
        order:playingmethod_sort,
    }, function (ajaxresult) {
		console.log(ajaxresult);
		if(ajaxresult.code==1){
			if((ajaxresult.data).length < limit){
                myApp.detachInfiniteScroll($$('#playingmethodlist .infinite-scroll'));
                $$('#playingmethodlist .infinite-scroll-preloader').hide();
            }else{
                myApp.attachInfiniteScroll($$('#playingmethodlist .infinite-scroll'));
                $$('#playingmethodlist .infinite-scroll-preloader').show();     
            }
			var data_playingmethodcontent = ajaxresult.data;
			var html= myApp.playItemTemplate(data_playingmethodcontent);
        
			if(type=='refresh'){
    		    $$('#playingmethodlist .div_playingmethodcontent').html(html);
    		    //更新last load index
      			var lastIndex=$$('.div_playingmethodcontent .playitem').length;
				$$("input[name='playingmethod_lastIndex']").val(lastIndex);
			}else if(type=='append'){
    			$$('#playingmethodlist .div_playingmethodcontent').append(html);
    			//更新last load index
    			var lastIndex=$$('.div_playingmethodcontent .playitem').length;
    			$$("input[name='playingmethod_lastIndex']").val(lastIndex);
    		}		
		}else{
			ajaxfail(ajaxresult.msg);
			$$('#playingmethodlist .div_playingmethodcontent').html('');
		}
    });
}

function getselfplaymethodlist(offset,limit,type){
	var selfplayingmethod_city='';
	//搜索条件
	$$('.selfplayingmethodcity a.active').each(function(){
		selfplayingmethod_city+=$$(this).data('id')+'|';
	});
	var selfplayingmethod_type = $$('.selfplayingmethodtype a.active').data('id');
	var selfplayingmethod_sort = $$('.selfplayingmethodsort a.active').data('id');	
	//收起subnav
	console.log(selfplayingmethod_city,selfplayingmethod_type,selfplayingmethod_sort);
	$$("#selfplayingmethodlist").removeClass('with-shadow');
	if ($$('.selfplayingmethodtype').hasClass('grade-w-roll')){
        $$('.selfplayingmethodtype').removeClass('grade-w-roll');
    };
    if ($$('.selfplayingmethodsort').hasClass('grade-w-roll')){
        $$('.selfplayingmethodsort').removeClass('grade-w-roll');
    };
    if ($$('.selfplayingmethodcity').hasClass('grade-w-roll')){
        $$('.selfplayingmethodcity').removeClass('grade-w-roll');
    };
    //刷新列表
    $$.getJSON(url_get_selfplayingmethodlist, {
        cityId:selfplayingmethod_city,
        offset:offset,
        limit:limit,
        type:selfplayingmethod_type,
        order:selfplayingmethod_sort
    }, function (ajaxresult) {
        console.log(ajaxresult);
        if(ajaxresult.code==1){
        	if((ajaxresult.data).length < limit){
                myApp.detachInfiniteScroll($$('#selfplayingmethodlist .infinite-scroll'));
                $$('#selfplayingmethodlist .infinite-scroll-preloader').hide();
            }else{
                myApp.attachInfiniteScroll($$('#playingmethodlist .infinite-scroll'));
                $$('#selfplayingmethodlist .infinite-scroll-preloader').show();     
            }
        	var data_selfplayingmethodlist = ajaxresult.data;
            var html = myApp.playSelfItemTemplate(data_selfplayingmethodlist);
        
			if(type=='refresh'){
    		    $$('#selfplayingmethodlist .div_selfplayingmethodcontent').html(html);
    		    //更新last load index
      			var lastIndex=$$('.div_selfplayingmethodcontent .self_playitem').length;
				$$("input[name='selfplayingmethod_lastIndex']").val(lastIndex);
			}else if(type=='append'){
    			$$('#selfplayingmethodlist .div_selfplayingmethodcontent').append(html);
    			//更新last load index
    			var lastIndex=$$('.div_selfplayingmethodcontent .self_playitem').length;
    			$$("input[name='selfplayingmethod_lastIndex']").val(lastIndex);
    		}		
		}else{
			ajaxfail(ajaxresult.msg);
			$$('#playingmethodlist .div_playingmethodcontent').html('');
		}
    });
}

function getscopelist(offset,limit,type){
	//搜素条件
	var scopecity = $$('.scopecity a.active').data('id');
	var scopetype = $$('.scopetype a.active').data('id');
	//收起subnav
	$$("#scopelist").removeClass('with-shadow');
	if ($$('.scopecity').hasClass('grade-w-roll')){
        $$('.scopecity').removeClass('grade-w-roll');
    };
    if ($$('.scopetype').hasClass('grade-w-roll')){
        $$('.scopetype').removeClass('grade-w-roll');
    };
    //刷新列表
	$$.getJSON(url_get_scopelist, {city:scopecity,offset:offset,limit:limit,type:scopetype}, function (ajaxresult) {
		if(ajaxresult.code==1){
			if((ajaxresult.data).length < limit){
                myApp.detachInfiniteScroll($$('#scopelist .infinite-scroll'));
                $$('#scopelist .infinite-scroll-preloader').hide();
            }else{
                myApp.attachInfiniteScroll($$('#scopelist .infinite-scroll'));
                $$('#scopelist .infinite-scroll-preloader').show();     
            }

			var data_scopelist = ajaxresult.data;
			var html = myApp.scopeListTemplate(ajaxresult.data);
			
			if(type=='refresh'){
    		    $$('#scopelist .div_scopelist').html(html);
    		    //更新last load index
      			var lastIndex=$$('.div_scopelist .scopeitem').length;
				$$("input[name='scope_lastIndex']").val(lastIndex);
			}else if(type=='append'){
    			$$('#scopelist .div_scopelist').append(html);
    			//更新last load index
    			var lastIndex=$$('.div_scopelist .scopeitem').length;
    			$$("input[name='scope_lastIndex']").val(lastIndex);
    		}               
		}else{
			ajaxfail(ajaxresult.msg);
			$$('#scopelist .div_scopelist').html('');
		}
	});
}
